<!-- 导航栏页面 -->
<template>
  <nav class="navbar">
    <!-- Logo -->
    <div class="navbar-logo">
      <img src="/images/result.jpeg" alt="Website Logo" />
    </div>

    <!-- 导航栏 -->
    <ul class="navbar-links">
      <li>
        <a
            href="/"
            class="nav-link"
            :class="{ 'active': activeComponent === 'home' }"
        >
          <i class="fas fa-home"></i> 首页
        </a>
      </li>
      <li class="dropdown">
        <a href="/destinations" class="nav-link" :class="{ 'active': activeComponent === 'destinations' }">
          <i class="fas fa-map-marker-alt"></i> 目的地
        </a>

      </li>
      <li class="dropdown">
        <!--        <a href="/travelGuidePage" class="nav-link" :class="{ 'active': activeComponent === 'strategy' }">-->
        <!--          <i class="fas fa-book-open"></i> 攻略 <i class="fas fa-chevron-down"></i>-->
        <!--        </a>-->
        <a href="/guideView" class="nav-link" :class="{ 'active': activeComponent === 'strategy' }">
          <i class="fas fa-book-open"></i> 攻略
        </a>

      </li>
      <li>
        <a
            href="/travelogues"
            class="nav-link"
            :class="{ 'active': activeComponent === 'travelogue' }"
        >
          <i class="fas fa-pen-fancy"></i> 游记
        </a>
      </li>
      <li>
        <a
            href="/plan"
            class="nav-link"
            :class="{ 'active': activeComponent === 'travels' }"
        >
          <i class="fas fa-users"></i> ai制定攻略
        </a>
      </li>
      <li>
        <a
            href="/mapContainer"
            class="nav-link"
            :class="{ 'active': activeComponent === 'about' }"
        >
          <i class="fas fa-info-circle"></i> 关于我们
        </a>
      </li>
    </ul>

    <!-- 搜索框 -->
    <div class="navbar-search">
      <div class="search-container">
        <i class="fas fa-search search-icon"></i>
        <input v-model="searchKeyword" type="text"   placeholder="搜索目的地、攻略、游记..." />
        <button type="button" @click="goToSearch" >搜索</button>
      </div>
    </div>

    <!-- 用户登录/注册 -->
    <div class="navbar-user">
      <!-- 已登录状态 -->
      <el-popover
          v-if="isLoggedIn"
          placement="bottom"
          width="220px"
          trigger="hover"
          popper-class="user-popover"
      >
        <!-- 悬浮窗内容 -->
        <template #reference>
          <el-avatar
              :size="40"
              :src="userInfo.avatar"
              class="cursor-pointer"
          />
        </template>

        <!-- 用户信息卡片 -->
        <div class="user-card">
          <!-- 顶部信息 -->
          <div class="flex items-center mb-4">
            <el-avatar :size="50" :src="userInfo.avatar" />
            <div class="ml-3">
              <span class="username">{{userInfo.userName }}</span>
              <span
                  :class="levelBadgeClass"
                  class="level-badge"
              >
      LV{{ userInfo.level }}
    </span>
            </div>
          </div>

          <!-- 数据统计 -->
          <div class="stats-grid">
            <div>
              <div class="text-sm text-gray-500">关注</div>
              <div class="font-medium">{{ userInfo.following }}</div>
            </div>
            <div>
              <div class="text-sm text-gray-500">粉丝</div>
              <div class="font-medium">{{ userInfo.followers }}</div>
            </div>
            <div>
              <div class="text-sm text-gray-500">动态</div>
              <div class="font-medium">{{ userInfo.posts }}</div>
            </div>
          </div>

          <!-- 功能入口 -->
          <div class="action-links" @click="goToProfile">
            <el-link type="info" class="text-sm">个人中心</el-link>
            <el-icon><ArrowRightBold /></el-icon>
          </div>

          <!-- 底部菜单 -->
          <el-divider class="my-4" />
          <div class="menu-list">
            <div v-for="item in menus" :key="item.text" class="menu-item">
              <i :class="item.icon"></i>
              <span>{{ item.text }}</span>
            </div>
          </div>
          <!-- 退出登录 -->
          <!-- 退出登录 -->
          <div class="action-links" @click="logout">
            <el-link type="info" class="text-sm">退出登录</el-link>
            <el-icon><SwitchButton /></el-icon>
          </div>

        </div>
      </el-popover>

      <!-- 未登录状态 -->
      <template v-else>
        <a href="/login" class="login-btn">登录</a>
        <a href="/register" class="register-btn">注册</a>
      </template>
    </div>
  </nav>
</template>

<script>
import { defineComponent, ref,onMounted , computed} from 'vue'
import { ElPopover, ElAvatar, ElButton } from 'element-plus'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex' //
import { SwitchButton } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'



export default defineComponent({
  name: 'NavBar',
  components: { ElPopover, ElAvatar},
  props: {
    activeComponent: {
      type: String,
      default: 'home'
    }
  },
  emits: ['update:activeComponent'],
  setup(props, { emit }) {
    // 用户状态数据
    const router = useRouter();
    const searchKeyword = ref('');

    const goToSearch = () => {
      console.log(`output-serggggggggggggggggggggggggggggggggggggggggggggggggggggggggggch`,searchKeyword.value)
      if (searchKeyword.value.trim()) {
        router.push({ path: '/search', query: { q: searchKeyword.value.trim() } });
      }
    };
    const store = useStore();
    const defaultUserInfo = {
      userId: 0,
      userName: "游客",
      realName: "游客",
      email: "",
      phone: "",
      gender: "",
      birthday: "",
      avatar: "/images/default-avatar.jpg",
      bCoin: 0,
      coin: 0,
      exp: 0,
      level: 0,
      password: "",
      status: 0
    };

    const userInfo = ref(store.state.userInfo || defaultUserInfo);
    const isLoggedIn = computed(() => !!store.state.userInfo)

    // 菜单配置（完全对应图像内容）

    const getLevelFromExp = (exp) => {
      if (exp < 1000) return 1;
      if (exp >= 1000 && exp < 5000) return 2;
      if (exp >= 5000 && exp < 10000) return 3;
      if (exp >= 10000 && exp < 20000) return 4;
      if (exp >= 20000) return 5;
      return 1; // 默认返回 1 级
    };
    const levelBadgeClass = computed(() => {
      switch (userInfo.value.level) {
        case 1:
          return 'level-badge level-1'; // 灰色
        case 2:
          return 'level-badge level-2'; // 绿色
        case 3:
          return 'level-badge level-3'; // 蓝色
        case 4:
          return 'level-badge level-4'; // 粉色
        case 5:
          return 'level-badge level-5'; // 金色
        default:
          return 'level-badge level-1'; // 默认灰色
      }
    });
    onMounted(() => {
      // 假设你想要为 userInfo 添加更多属性
      userInfo.value = {
        ...userInfo.value,
        level: getLevelFromExp(userInfo.value.exp), // 更新或者添加的属性
        coin: 500,
        bCoin: 50
      }
      console.log(`output->userInfo.value`,userInfo.value)
      updateUserInfo();
    })
    const menuItems = [
      { icon: 'el-icon-user', label: '个人中心', action: 'profile' },
      { icon: 'el-icon-document', label: '投稿管理', action: 'posts' },
      { icon: 'el-icon-star-off', label: '推荐服务', action: 'recommend' },
      { icon: 'el-icon-switch-button', label: '退出登录', action: 'logout' }
    ]

    // 事件处理
    const handleNavClick = (component) => {
      emit('update:activeComponent', component)
    }
    const updateUserInfo = () => {
      emit('updateUserInfo', userInfo.value);
    };
    const handleMenuClick = (action) => {
      if (action === 'logout') {
        isLoggedIn.value = false
        // 实际项目需添加退出逻辑
      }
    }



    const goToProfile = () => {
      router.push('/personView') // 跳转到名为 'Profile' 的路由
    }

    const logout = async () => {
      await store.dispatch('logout')
      ElMessage.success('退出登录成功！')
    }

    return {
      updateUserInfo,
      handleNavClick,
      handleMenuClick,
      isLoggedIn,
      userInfo,
      menuItems,
      goToProfile,levelBadgeClass,logout,goToSearch,searchKeyword
    }
  }
})
</script>


<style scoped>
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@300;400;500;700&display=swap');
@import url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css');

.navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 15px 40px;
  background-color: #ffffff;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  font-family: 'Noto Sans SC', sans-serif;
  position: sticky;
  top: 0;
  z-index: 1000;
}

.navbar-logo img {
  height: 45px;
  transition: transform 0.3s ease;
}

.navbar-logo img:hover {
  transform: scale(1.05);
}

.navbar-links {
  display: flex;
  list-style: none;
  margin: 0;
  padding: 0;
}

.navbar-links li {
  position: relative;
  margin: 0 12px;
}

.nav-link {
  text-decoration: none;
  color: #3a3a3a;
  font-weight: 500;
  font-size: 15px;
  padding: 10px 15px;
  border-radius: 20px;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;
}

.nav-link i {
  font-size: 14px;
}

.nav-link:hover, .nav-link.active {
  color: #0e9aa7;
  background-color: rgba(14, 154, 167, 0.08);
}

.dropdown-menu {
  display: none;
  position: absolute;
  top: 110%;
  left: 50%;
  transform: translateX(-50%);
  background-color: #ffffff;
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
  list-style: none;
  padding: 10px 0;
  min-width: 180px;
  border-radius: 8px;
  z-index: 1000;
  opacity: 0;
  transition: opacity 0.3s ease, top 0.3s ease;
}

.dropdown-menu::before {
  content: '';
  position: absolute;
  top: -5px;
  left: 50%;
  transform: translateX(-50%);
  border-left: 7px solid transparent;
  border-right: 7px solid transparent;
  border-bottom: 7px solid #ffffff;
}

.dropdown-menu li {
  margin: 5px 0;
}

.dropdown-menu a {
  display: block;
  padding: 10px 20px;
  color: #3a3a3a;
  text-decoration: none;
  font-size: 14px;
  transition: all 0.2s ease;
  border-radius: 4px;
  margin: 0 8px;
}

.dropdown-menu a:hover {
  background-color: #f8f9fa;
  color: #0e9aa7;
  transform: translateX(5px);
}

.dropdown:hover .dropdown-menu {
  display: block;
  top: 100%;
  opacity: 1;
}

.search-container {
  position: relative;
  display: flex;
  align-items: center;
  background-color: #f5f7fa;
  border-radius: 24px;
  padding: 0 5px;
  transition: all 0.3s ease;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.03);
}

.search-container:focus-within {
  box-shadow: 0 0 0 2px rgba(14, 154, 167, 0.3);
}

.search-icon {
  margin-left: 15px;
  color: #8a8a8a;
}

.navbar-search input {
  padding: 12px 12px 12px 10px;
  border: none;
  background-color: transparent;
  width: 250px;
  font-size: 14px;
  outline: none;
  color: #3a3a3a;
}

.navbar-search input::placeholder {
  color: #a8a8a8;
}

.navbar-search button {
  padding: 10px 20px;
  background-color: #0e9aa7;
  color: #ffffff;
  border: none;
  border-radius: 20px;
  cursor: pointer;
  font-weight: 500;
  transition: all 0.3s ease;
  margin-left: 5px;
}

.navbar-search button:hover {
  background-color: #057885;
  transform: translateY(-2px);
}

.navbar-user {
  display: flex;
  align-items: center;
  gap: 10px;
}

.user-btn {
  text-decoration: none;
  font-weight: 500;
  font-size: 14px;
  transition: all 0.3s ease;
  padding: 8px 16px;
  border-radius: 20px;
  display: flex;
  align-items: center;
  gap: 5px;
}

.login-btn {
  color: #3a3a3a;
  border: 1px solid #e0e0e0;
}

.login-btn:hover {
  background-color: #f5f7fa;
  color: #0e9aa7;
  border-color: #f0f0f0;
}

.register-btn {
  background-color: #0e9aa7;
  color: black;
}

.register-btn:hover {
  background-color: #057885;
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(14, 154, 167, 0.2);
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .navbar {
    padding: 12px 20px;
  }

  .nav-link {
    padding: 8px 10px;
    font-size: 14px;
  }

  .navbar-search input {
    width: 200px;
  }
}
/*后来后来写的*/
/* ================= 用户卡片 ================= */
.user-card {
  padding: 16px;
  background-color: #ffffff; /* 确保用户卡片背景为白色 */
  border: 1px solid #ddd;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  margin-bottom: 16px;
  position: relative;
  text-align: center; /* 使内部元素居中对齐 */
}
.username {
  margin-right: 8px; /* 调整用户名和等级徽章之间的间距 */
  font-size: 16px;
  font-weight: bold;
}
/* ================= 等级徽章 ================= */
.level-badge {
  color: #fff;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 12px;
  line-height: 1.5;
  position: absolute;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.level-1 {
  background: #b0bec5; /* 灰色 */
}

.level-2 {
  background: #66bb6a; /* 绿色 */
}

.level-3 {
  background: #42a5f5; /* 蓝色 */
}

.level-4 {
  background: #fb7299; /* 粉色 */
}

.level-5 {
  background: #ffb300; /* 金色 */
}
/* ================= 数据统计网格 ================= */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
  margin: 16px 0;
  border-top: 1px solid #eee;
  border-bottom: 1px solid #eee;
  padding: 12px 0;
}

.stats-grid > div {
  text-align: center;
  background: #f8f9fa;
  padding: 8px;
  border-radius: 6px;
  font-weight: 500;
}

.stats-grid > div > div:first-child {
  color: #666;
  font-size: 12px;
  margin-top: 4px;
}

/* ================= 菜单交互效果 ================= */
.menu-item {
  display: flex;
  align-items: center;
  padding: 10px 16px;
  border-radius: 6px;
  transition: background 0.2s ease;
  cursor: pointer;
}

.menu-item:hover {
  background: #f5f5f5;
}

.menu-item i {
  margin-right: 12px;
  color: #666;
  width: 20px;
  text-align: center;
}

/* ================= 登录按钮 ================= */
.login-btn {
  background: #00a1d6; /* 更接近图像中 B 站蓝色的色调 */
  color: white;
  padding: 8px 20px;
  border-radius: 4px;
  transition: background 0.2s;
  border: none;
  cursor: pointer;
}

.login-btn:hover {
  background: #0092c4;
}

.register-btn {
  border: 1px solid #e7e7e7;
  padding: 8px 20px;
  border-radius: 4px;
  margin-left: 12px;
  transition: background 0.2s;
  background: #ffffff;
  cursor: pointer;
}

.register-btn:hover {
  background: #f8f9fa;
}

.action-links {
  display: flex;
  justify-content: space-between; /* 左边为文字，右边为箭头 */
  align-items: center;
  padding: 0 8px; /* 可选：调整内边距 */
  text-align: left; /* 确保文本左对齐 */
}

.arrow {
  font-size: 16px;  /* 根据需要调整箭头大小 */
  color: inherit;   /* 使用与链接一致的颜色，也可以自定义颜色 */
}

/* ================= 适配图像细节 ================= */
/* 会员按钮渐变效果 */
.vip-button {
  background: linear-gradient(135deg, #ff8eb3 0%, #fb7299 100%);
  border: none;
  color: #fff;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
}

.vip-button:hover {
  opacity: 0.9;
}

/* 硬币数值特殊样式 */
.coin-value {
  color: #ff9500;
  font-weight: bold;
}



</style>